package net.anzhuo.controller.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsPriority;
import net.anzhuo.entity.GsQuery;
import net.anzhuo.entity.db.syncdata.PairValue;
import net.anzhuo.entity.db.syncdata.QueryKeyNoResult;
import net.anzhuo.entity.db.syncdata.UpdateKeyNoResult;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsPriorityService;
import net.anzhuo.service.GsQueryService;


@RestController
@Slf4j
public class SyncKeyNoListController {

	@Autowired
	private GsPriorityService gsPriorityService;

	@Autowired
	private GsBaseService gsBaseService;
	
	
	@Autowired
	private GsQueryService gsQueryService;
		
	
	@RequestMapping("/KeyNoList")
	public QueryKeyNoResult KeyNoList(String status,String statusBasic,String statusJudge) {
		log.info("===>KeyNoList="+status);
		long startTime=System.currentTimeMillis();
		QueryKeyNoResult qknr=new QueryKeyNoResult();		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsPriority gp=new GsPriority();
			gp.setStatus(status);
			gp.setStatusBasic(statusBasic);
			gp.setStatusJudge(statusJudge);
		   List<GsPriority> list=gsPriorityService.getBycoMd5(gp);
		  
		   List<PairValue> keyNoList=new ArrayList<PairValue>();
		   PairValue pv=new PairValue();
		   for(GsPriority g:list)
		   {
			  
			   pv=new PairValue();
			   pv.setKeyNo(g.getKeyNo());
			   if(gsBaseService.getByMd5(g.getKeyNo())!=null)
					   {
				   if(gsBaseService.getByMd5(g.getKeyNo()).getName()!=null&&!gsBaseService.getByMd5(g.getKeyNo()).getName().equals(""))
			       {
					   pv.setName(gsBaseService.getByMd5(g.getKeyNo()).getName());
					   pv.setCreditCode(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode());
					   log.info("Name not null="+g.getKeyNo()+":"+pv.getName());
			       }else
					   {
			    	   if(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode()!=null&&!gsBaseService.getByMd5(g.getKeyNo()).getCreditCode().equals(""))
			    	   {
			    		  // pv.setName(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode());
			    		  // log.info("Name is null and credit is not null="+pv.getName());
			    	   }else
			    	   {
			    		   continue;
			    	   }
			    	   }
					   }	   
			   keyNoList.add(pv); 
		   }
		   qknr.setCount(keyNoList.size());
		   log.info("size="+keyNoList.size());
		   qknr.setKeyNoList(keyNoList);
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}	
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" KeyNoList 执行时间："+excTime+"s");
		return qknr;
	}
	
	
	@RequestMapping("/QueryNotExistsList")
	public QueryKeyNoResult QueryNotExistsList(String status) {
		log.info("===>QueryNotExistsList="+status);
		long startTime=System.currentTimeMillis();
		QueryKeyNoResult qknr=new QueryKeyNoResult();		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsQuery gp=new GsQuery();
			gp.setStatus(status);
		   List<GsQuery> list=gsQueryService.getList(gp);
		   log.info("list's size="+list.size());
		   List<PairValue> keyNoList=new ArrayList<PairValue>();
		   PairValue pv=new PairValue();
		   for(GsQuery g:list)
		   {			  
			   pv=new PairValue();
			   GsBase   gsBase=gsBaseService.getByCreditCode(g.getCreditCode());
			   if(gsBase==null)
			   {
				 pv.setCreditCode(g.getCreditCode());   
				 pv.setName(g.getName());
				 pv.setKeyNo(g.getKeyNo());
			   }else
			   {				
				pv.setName(g.getName());
			   }			  
			   keyNoList.add(pv); 
		   }
		   qknr.setCount(keyNoList.size());
		   log.info("size="+keyNoList.size());
		   qknr.setKeyNoList(keyNoList);
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}	
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" KeyNoList 执行时间："+excTime+"s");
		return qknr;
	}
	
	
	

	@RequestMapping("/UpdateQuery")
	public UpdateKeyNoResult UpdateQuery(String name,String creditCode,String keyNo, String status) {
		log.info("===>UpdateQuery="+name+",creditCode="+creditCode+",status="+status);
		long startTime=System.currentTimeMillis();
		UpdateKeyNoResult qknr=new UpdateKeyNoResult();
		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsQuery gp=new GsQuery();
			gp.setKeyNo(keyNo);
			gp.setStatus(status);
			gp.setCreditCode(creditCode);
			gp.setName(name);
			Date date= new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(date);
			
			gp.setUpdateTime(str);
		   gsQueryService.update(gp);
		 
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}
		
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" UpdateQuery 执行时间："+excTime+"s");
		return qknr;
	}
	
	@RequestMapping("/UpdateKeyNo")
	public UpdateKeyNoResult UpdateKeyNoList(String keyNo,String status,String statusBasic,String statusJudge) {
		log.info("===>UpdateKeyNo="+keyNo);
		long startTime=System.currentTimeMillis();
		UpdateKeyNoResult qknr=new UpdateKeyNoResult();
		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsPriority gp=new GsPriority();
			gp.setKeyNo(keyNo);
			gp.setStatus(status);
			gp.setStatusBasic(statusBasic);
			gp.setStatusJudge(statusJudge);
			Date date= new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str = sdf.format(date);
			
			gp.setOperdate(str);
		   gsPriorityService.update(gp);
		 
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}
		
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" UpdateKeyNo 执行时间："+excTime+"s");
		return qknr;
	}
	
	
}
